using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class Ballon : MonoBehaviour
{
    private Rigidbody2D rb;

    private float floatStrenth = 1f;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        
        transform.Rotate(0,0,Random.Range(-15f,15f));
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        WindController wind = FindObjectOfType<WindController>();
        if (wind != null)
        {
            rb.AddForce(wind.windForce);

            float swing = Mathf.Sin(Time.time * 2f) * 5f;
            transform.rotation = Quaternion.Euler(0, 0, swing);

            rb.AddForce(Vector2.up * floatStrenth);
        }
    }

    private void Update()
    {
        Vector2 screenPos = Camera.main.WorldToViewportPoint(transform.position);
        if (screenPos.y > 1.2f || Mathf.Abs(screenPos.x) > 1.5f) {
            Destroy(gameObject);
        }
    }
}
